

void main() {
  var safeId = IdNumber(42424242);
  var newSafeId = safeId + 10; // 现在可以使用 + 操作符
  print(newSafeId); // 输出: IdNumber(42424252)

  // 如果需要将 IdNumber 转换为 int
  int myUnsafeId = safeId.value;
  print(myUnsafeId); // 输出: 42424242




  var e1 = ES(4);
  var e2 = ES.n(3);
  var e3 = ES.m(5, "Hello!");

  e1.printValue(); // 输出: The value is 4
  e2.printValue(); // 输出: The value is 3
  e3.printValue(); // 输出: The value is 11



// All OK: Transparency allows invoking `int` members on the extension type:
  var v1 = NumberT(1); // v1 type: NumberT
  int v2 = NumberT(2); // v2 type: int
  var v4 = v2 + v1; // v4 type: int
  var v5 = 2 + v1; // v5 type: int
  // Error: Extension type interface is not available to representation type
  


//https://dart.cn/language/extension-types/#type-considerations
 var n = NumberE(1);

  // Run-time type of 'n' is representation type 'int'.
  if (n is int) print(n.value); // Prints 1.

  // Can use 'int' methods on 'n' at run time.
  if (n case int x) print(x.toRadixString(10)); // Prints 1.
  switch (n) {
    case int(:var isEven): print("$n (${isEven ? "even" : "odd"})"); // Prints 1 (odd).
  }

int i = 2;
  if (i is NumberE) print("It is"); // Prints 'It is'.
  if (i case NumberE v) print("value: ${v.value}"); // Prints 'value: 2'.
  switch (i) {
    case NumberE(:var value): print("value: $value"); // Prints 'value: 2'.
  }




}




class IdNumber {
  final int value;

  IdNumber(this.value);

  // 重载 + 操作符
  IdNumber operator +(int other) {
    return IdNumber(value + other);
  }

  // 可选：重载 toString 方法以便更好地打印
  @override
  String toString() {
    return 'IdNumber($value)';
  }
}





//https://dart.cn/language/extension-types/#declaration
extension type E(int i) {
  // Define set of operations.
}



//https://dart.cn/language/extension-types/#declaration
extension type ES(int i) {
  ES.n(this.i);
  ES.m(int j, String foo) : i = j + foo.length;

  // 添加一个方法
  void printValue() {
    print('The value is $i');
  }
}




//https://dart.cn/language/extension-types/#members
extension type NumberE(int value) {
  // 定义 isNegative 方法
  bool isNegative() => value < 0;
  // Operator:
  NumberE operator +(NumberE other) =>
      NumberE(value + other.value);
  // Getter:
  NumberE get myNum => this;
  // Method:
  bool isValid() => !value.isNegative;



  // 定义 - 操作符
  NumberE operator -(NumberE other) => NumberE(value - other.value);

  

  // 定义 next getter
  NumberE get next => NumberE(value + 1);

  
}


//https://dart.cn/language/extension-types/#implements
extension type NumberI(int i) 
  implements int{
  // 'NumberI' can invoke all members of 'int',
  // plus anything else it declares here.

}


extension type Sequence<T>(List<T> _) implements Iterable<T> {
  // Better operations than List.
}

extension type Id(int _id) implements Object {
  // Makes the extension type non-nullable.
  static Id? tryParse(String source) => int.tryParse(source) as Id?;
}

extension type const Opt<T>._(({T value})? _) { 
  const factory Opt(T value) = Val<T>;
  const factory Opt.none() = Non<T>;
}
extension type const Val<T>._(({T value}) _) implements Opt<T> { 
  const Val(T value) : this._((value: value));
  T get value => _.value;
}
extension type const Non<T>._(Null _) implements Opt<Never> {
  const Non() : this._(null);
}

extension type MyString(String _) implements String {
  // Replaces 'String.operator[]'
  
  int operator [](int index) => codeUnitAt(index);
}


//https://dart.cn/language/extension-types/#usage

extension type NumberEs(int value) {
  NumberE operator +(NumberE other) =>
      NumberE(value + other.value);

  NumberE get next => NumberE(value + 1);
  bool isValid() => !value.isNegative;
}

void testE() { 
  var num = NumberEs(1);
}


//https://dart.cn/language/extension-types/#1-provide-an-extended-interface-to-an-existing-type
extension type NumberT(int value) 
  implements int {
  // Doesn't explicitly declare any members of 'int'.
  NumberT get i => this;

// 定义 isNegative 方法
  bool isNegative() => value < 0;

  // 定义 - 操作符
  NumberE operator -(NumberE other) => NumberE(value - other.value);

  // 定义 + 操作符，支持 NumberE 和 int
  NumberE operator +(dynamic other) {
    if (other is NumberE) {
      return NumberE(value + other.value);
    } else if (other is int) {
      return NumberE(value + other);
    } else {
      throw ArgumentError('Unsupported type');
    }
  }

  // 定义 next getter
  NumberE get next => NumberE(value + 1);


}




//https://dart.cn/language/extension-types/#2-provide-a-different-interface-to-an-existing-type

void testES() {
 var num1 = NumberE(1);
  int num2 = num1.value; // 访问底层值

  num1.isValid(); // 假设 isValid 是扩展方法
  print(num1.isNegative()); // 输出: false

  var sum1 = num1 + num1; // 使用 + 操作符
  var diff1 = num1 - num1; // 使用 - 操作符
  var diff2 = num1.value - 2; // 访问底层值
  

  List<NumberE> numbers = [
    NumberE(1),
    num1.next, // 使用 next getter
  ];

  // 打印结果以验证
  print(sum1.value); // 输出: 2
  print(diff1.value); // 输出: 0
  print(diff2); // 输出: -1
  print(numbers.map((n) => n.value).toList()); 
}
